<template>
	<view class="container">
		<!-- <video :src="video.video" style="height: 90vh; width: 100vw;"></video> -->
			<video :src="video.video" style="height: 90vh;width: 100vw;"></video>
			<view class=""
				style="width: 100%; height: 10vh; display: flex; justify-content: space-around; align-items: center;">
				<view
					style="width: 300rpx; height: 80rpx; line-height: 80rpx; text-align: center; background-color: darkgrey; color: white; border-radius: 50rpx;"
					@click="deleteVideo(video.id)">删除</view>
				<view
					style="width: 300rpx; height: 80rpx; line-height: 80rpx; text-align: center; background-color: rgb(14, 16, 101); color: white; border-radius: 50rpx;"
					@click="showDownloadTip">下载到相册</view>
			</view>
	</view>
</template>

<script>
	import Http from "@/utils/http"
	const http = new Http()
	export default {
		data() {
			return {
				videoPath: '',
				url: '',
				video: {},
				id: '',
				src: ""
			}
		},
		onUnload() {
			uni.$emit('can', this.id);
		},
		onLoad(option) {
			uni.setNavigationBarTitle({
				title: '预览爆款视频',
			})
			this.getInfo(option.id)
			this.id = option.id;
		},
		// onUnload() {
		//     uni.redirectTo({
		//         url: '/subpkg/mkvideo/myJianjiKu'
		//     });
		// },
		methods: {
			deleteVideo(id) {
				http.get('v1/jiaoben/delete/' + id).then(res => {
					if (res.data.code == 200) {
						uni.navigateTo({
							url: "/subpkg/mkvideo/darenkoubo"
						})
						uni.showToast({
							title: '删除成功',
							icon: 'success'
						})
					}
				})
			},

			getInfo(id) {
				http.get('v1/jiaoben/' + id).then(res => {

					this.video = res.data.data;
				})
			},

			showDownloadTip(event) {
				this.downFile()

			},
			saveImageToPhotosAlbum() {
				let that = this;
				console.log(that);

				uni.request({
					url: 'saveImageToPhotosAlbum',
					success: (res) => {
						uni.showModal({
							title: '下载视频',
							content: '是否保存视频到本地？',
							success(res) {
								if (res.confirm) {
									// that.downFile()
									console.log("===>");
								}
							}
						});

					},
					fail: function(err) {

					}

				});
			},
			downFile() {
				let that = this
				uni.downloadFile({
					url: that.video.video,
					success: function(res) {
						if (res.statusCode === 200) {
							uni.showLoading({
								title: "正在下载中"
							})
							uni.saveVideoToPhotosAlbum({
								filePath: res.tempFilePath,
								success: function() {
									uni.showToast({
										title: '视频保存成功',
										icon: 'success',
										duration: 2000
									});
									http.post("v1/jiaoben/deleteBaocun", {
										id: that.id,
										video: that.video.video
									}).then(res => {
										if (res.data.code != 200) {
											return uni.showToast({
												title: res.data.msg
											})
										}
										uni.showToast({
											title: "删除成功"
										})
										uni.navigateBack({
											delta: 2
										})

									})
								},
								fail: function(error) {
									console.log(error);
									// 如果保存视频权限被拒绝，提醒用户需要授权才能保存视频
									if (error.errMsg.indexOf(
											'saveVideoToPhotosAlbum:fail auth deny') !== -1) {
										uni.showModal({
											title: '提示',
											content: '保存视频需要您的授权，请点击确定重新授权',
											success: function(ress) {
												if (ress.confirm) {
													uni.openSetting({
														success(settingdata) {
															if (settingdata
																.authSetting[
																	'scope.writePhotosAlbum'
																	]) {
																uni.showModal({
																	title: '提示',
																	content: '获取权限成功,再次保存图片即可成功',
																	showCancel: false,
																})
															} else {
																uni.showModal({
																	title: '提示',
																	content: '获取权限失败，无法保存到相册',
																	showCancel: false
																})
															}
														}
													})

													// 重新请求保存相册权限
													// uni.saveVideoToPhotosAlbum({
													// 	filePath: res
													// 		.tempFilePath,
													// 	success: function() {
													// 		uni.showToast({
													// 			title: '视频保存成功',
													// 			icon: 'success',
													// 			duration: 2000
													// 		});
													// 	},
													// 	fail: function(error) {
													// 		console.log(
													// 			error);
													// 		uni.showToast({
													// 			title: '保存失败，请重试',
													// 			icon: 'none',
													// 			duration: 2000
													// 		});
													// 	}
													// });
												}
											}
										});
									}
								},
								complete: function(error) {
									uni.hideLoading()
								}


							});
						} else {
							console.log('下载失败');
						}
					},
					fail: function(error) {
						console.log(error);


					}
				});
			},
			e() {
				uni.openSetting({
					success(res) {
						console.log(res.authSetting)
						uni.showToast({
							icon: 'success',
							title: '授权成功'
						})
					}
				});
			},
			download() {
				const _this = this
				uni.getSetting({
					success(res) {
						if (!res.authSetting['scope.writePhotosAlbum']) {
							uni.authorize({
								scope: 'scope.writePhotosAlbum',
								success(res) {
									console.log(res, '同意授权');
									_this.saveImage()
								},
								fail(err) {
									console.log(err, '拒绝授权');
									uni.showModal({
										title: '提示',
										content: '检测到您没有授权保存图片到相册，为了更好的体验，请前往设置授权',
										success: function(res) {
											if (res.confirm) {
												_this.e()
											} else if (res.cancel) {
												console.log('用户点击取消');

											}
										}
									});
								}
							})
						} else {
							console.log('已经授权');
							_this.saveImage()
						}
					}
				});
			},
			saveImage() {
				const _this = this
				uni.downloadFile({
					url: that.video.video,
					success: function(res) {
						console.log(res);
						if (res.statusCode === 200) {
							uni.saveImageToPhotosAlbum({
								filePath: res.tempFilePath,
								success: function(res) {
									uni.showToast({
										icon: 'success',
										title: '保存成功'
									})
								},
								fail: function(err) {
									console.log(err);
									uni.showToast({
										icon: 'error',
										title: '保存失败'
									})
								}
							});

						}



					},
					fail: function(err) {
						console.log(err);
					}
				});

			}
		}
	}
</script>

<style>
	.container {}
</style>